package com.example.wangkai.firstmap;


import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private MapView mapView;
    //管理地图
    BaiduMap baiduMap;
    //定位
    private LocationClient mLocationClient;
    boolean isFirstMap = true;
    BitmapDescriptor bitmap;
    double latitude;
    double longitude;
    private int radius;
    private PoiSearch mPoiSearch;
    private OnGetPoiSearchResultListener poiListener;
    private ListView lvLocNear;
    private LocNearAddressAdapter adapter;
    private List<PoiInfo> nearList;
    private int checkPosition;
    private int locType;
    private String province;// 省份信息
    private String city;// 城市信息
    private String district;// 区县信息
    private float direction;// 手机方向信息
    List<String> jiedaoName = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        initViews();
        //定位
        initLocation();

        searchNearBy();
        nearList = new ArrayList<PoiInfo>();
        adapter = new LocNearAddressAdapter(this,nearList);
        poiListener = new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                if (poiResult != null) {
                    if (poiResult.getAllPoi() != null && poiResult.getAllPoi().size() > 0) {
                        nearList.addAll(poiResult.getAllPoi());

                        lvLocNear.setAdapter(adapter);
                    }
                }
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }
        };

    }

    private void initLocation() {
        //让框架中的接口指向实现类
        MyBdlocationListener myBdlocationListener = new MyBdlocationListener();
        mLocationClient = new LocationClient(getApplicationContext());
        mLocationClient.registerLocationListener(myBdlocationListener);
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span = 1000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤GPS仿真结果，默认需要\

        mLocationClient.setLocOption(option);
        mLocationClient.start();

    }

    private void initViews() {
        mapView = (MapView) findViewById(R.id.bmapView);
        lvLocNear = (ListView) findViewById(R.id.listview);
    }


    class MyBdlocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //纬度
            latitude = bdLocation.getLatitude();
            //经度
            longitude = bdLocation.getLongitude();
            //Status状态
            //坐标点
            LatLng currentLocation = new LatLng(latitude, longitude);
            //地图缩放的级别4-17 17地图显示的详细
            int zoom = 17;
            //移动地图
            if (isFirstMap) {
                MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngZoom(currentLocation, zoom);
                //以动画的方式移动地图
                baiduMap.animateMapStatus(mapStatusUpdate);
                //添加图片
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(currentLocation);

                bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka);
                markerOptions.icon(bitmap);
                markerOptions.draggable(true);
                baiduMap.addOverlay(markerOptions);
                isFirstMap = false;
            }
            if(bdLocation.hasRadius()){
                radius= (int) bdLocation.getRadius();
            }

            direction=bdLocation.getDirection();
            province=bdLocation.getProvince();
            city=bdLocation.getCity();
            district=bdLocation.getDistrict();

            //将当前位置加入list里面
            PoiInfo info=new PoiInfo();
            info.address=bdLocation.getAddrStr();
            info.city=bdLocation.getCity();
            info.location=currentLocation;
            info.name=bdLocation.getAddrStr();
            jiedaoName.add(info.name);
            nearList.add(info);
            adapter.notifyDataSetChanged();

            // 构造定位数据
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(bdLocation.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(latitude)
                    .longitude(longitude).build();
            baiduMap.setMyLocationData(locData);

        }

    }
    /**
     * 搜索周边
     * */
    private void searchNearBy() {
        mPoiSearch = PoiSearch.newInstance();
        PoiNearbySearchOption option = new PoiNearbySearchOption();
        mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
        option.keyword("美食");
        option.pageNum(10);
        option.radius(1000);
        option.sortType(PoiSortType.distance_from_near_to_far);
        option.location(new LatLng(latitude, longitude));
        option.pageCapacity(20);
        mPoiSearch.searchNearby(option);
        mPoiSearch.destroy();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理

        mapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
    }









}



